<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">名称</label>
        <el-input v-model="query.name" clearable placeholder="名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">类型</label>
        <el-select v-model="query.type" clearable filterable placeholder="请选择" class="filter-item">
          <el-option
            v-for="item in dict.power_type"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>

        <el-date-picker
          v-model="query.datetime"
          :default-time="['00:00:00', '23:59:59']"
          type="daterange"
          range-separator=":"
          size="small"
          class="date-item"
          value-format="yyyy-MM-dd HH:mm:ss"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <div class="formImg" align="center">
          <img :src="imgSrc"></div>
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" style="width: 270px;" @input="getImage()" />
          </el-form-item>
          <el-form-item label="类型" prop="type">
            <el-select v-model="form.type" filterable placeholder="请选择" style="width: 270px;">
              <el-option
                v-for="item in dict.power_type"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="时间" prop="datatime">
            <el-date-picker v-model="form.datetime" type="datetime" style="width: 270px;" />
          </el-form-item>
          <el-form-item label="电量" prop="power">
            <el-input v-model="form.power" style="width: 270px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column prop="name" label="名称" align="center" />
        <el-table-column prop="type" label="类型" align="center">
          <template slot-scope="scope">
            {{ dict.label.power_type[scope.row.type] }}
          </template>
        </el-table-column>
        <el-table-column prop="power" label="电量" align="center" />
        <el-table-column prop="datetime" label="时间" align="center" />
        <el-table-column v-if="checkPer(['admin','power:edit','power:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
      <!-- <Line3D /> -->
    </div>
  </div>
</template>

<script>
// import Line3D from '@/components/Echarts/Line3D'
import crudPower from '@/api/power'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, name: null, type: null, datetime: null, power: null }
export default {
  name: 'Power',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['power_type'],
  cruds() {
    return CRUD({ title: '电量', url: 'api/power', idField: 'id', sort: 'datetime,asc', crudMethod: { ...crudPower }})
  },
  data() {
    return {
      imgSrc: '',
      permission: {
        add: ['admin', 'power:add'],
        edit: ['admin', 'power:edit'],
        del: ['admin', 'power:del']
      },
      rules: {
      },
      queryTypeOptions: [
        { key: 'name', display_name: '名称' },
        { key: 'type', display_name: '类型' }
      ]
    }
  },
  mounted() {

  },
  methods: {
    getImage() {
      crudPower.getBase64(this.form.name).then(res => {
        this.imgSrc = res
      })
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.beforeToCU]() {
      this.getImage()
      return true
    }
  }
}
</script>

<style scoped>
  ::v-deep .el-input-number .el-input__inner {
  text-align: left;
}
.formImg{
    position: fixed;
    top: 10px;
    right: 18px;
    z-index: 10;
    background-color: black;

}

</style>
